package com.itqianfeng.smartmeeting.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itqianfeng.smartmeeting.config.DbUser;
import com.itqianfeng.smartmeeting.entity.User;
import com.itqianfeng.smartmeeting.mapper.UserMapper;
import com.itqianfeng.smartmeeting.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private DbUser dbUser;
    @Autowired
    private UserMapper userMapper;
    @Override
    public Integer saveUserDetails(User user) {
        //判断用户名是否存在
        QueryWrapper<User> w=new QueryWrapper<>();
        w.eq("user_name", user.getUserName());
        User user1 = userMapper.selectOne(w);
        if (user1!=null){
            return 2;
        }
        //生成Security的用户对象
      UserDetails userDetails= org.springframework.security.core.userdetails.User
              .withDefaultPasswordEncoder()
              .username(user.getUserName())
              .password(user.getPassWord())
              .roles(user.getUserRole())
              .build();
              dbUser.createUser(userDetails);
              //将生成的加密密码存入数据库
              user.setPassWord(userDetails.getPassword());
            int insert = userMapper.insert(user);
            return insert;
    }
}
